/*
 * @页面名称:窗体模型下报表系统构建类
 * @描述: 主要用于主页面中
 * @作者: 研发中心
 * @Date: 2019-11-01 08:57:47
 */
import { useDynamicStore } from "@/stores/modules/dynamic";
import { useCatalogStore } from "@/stores/modules/catalog";
import { useFondsStore } from "@/stores/modules/fonds";
import vueAxios from "@/api/index";
import { getSysConfig } from "@/utils/util";
class ReportForm {
  paramter: any;
  dynamicStore: any;
  constructor() {
    this.paramter = {
      List: "", // 存放数据源 JSON格式化后的数据
      ConfigGuid: null, // 模板id
      RowNumber: null, // 传输文件数量
      FM_ID: null,
      // TableName: "",
      Constants: [
        // 常量
        {
          Name: "全宗号",
          Value: ""
        },
        {
          Name: "全宗名",
          Value: ""
        },
        {
          Name: "门类代号",
          Value: ""
        },
        {
          Name: "门类名称",
          Value: ""
        },
        {
          Name: "分类名称",
          Value: ""
        },
        {
          Name: "分类号",
          Value: ""
        },
        {
          Name: "卷内数量",
          Value: ""
        },
        {
          Name: "附件数量",
          Value: ""
        },
        {
          Name: "Parent",
          Value: ""
        }
      ]
    };
    this.dynamicStore = useDynamicStore();
    this.catalogStore = useCatalogStore();
    this.fondsStore = useFondsStore();
  }
  /**
   * @名字:报表系统分析方法
   * @详情:
   * @参数:obj代表当前点击的报表模板数据对象
   * @返回值:
   */
  async analyseRepot(obj: any, type: string) {
    const numObj = { juannei: "", fujian: "" }; // 代表卷内数量和附件数量
    try {
      const { RM_ID, RM_Config } = obj;
      const { tableData, tableSelect, childTableData, childTableSelect, fulltextData } = this.dynamicStore;
      this.paramter.ConfigGuid = RM_ID;
      const res = JSON.parse(RM_Config);
      switch (res.DataSource) {
        case "本表父级":
          if (type === "folder") {
            if (tableData.length > 0 && tableSelect.length === 0) {
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = tableData.map(v => v.Record_ID);
            } else if (tableData.length > 0 && tableSelect.length > 0) {
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = tableSelect.map(v => v.Record_ID);
            }
          } else if (type === "file") {
            if (childTableData.length > 0 && childTableSelect.length === 0) {
              numObj.juannei = childTableData.length;
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = childTableData.map(v => v.Record_ID);
            } else if (childTableData.length > 0 && childTableSelect.length > 0) {
              numObj.juannei = childTableSelect.length;
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = childTableSelect.map(v => v.Record_ID);
            }
            if (childTableSelect.length > 0 && childTableData.length > 0) {
              // 父级json串
              this.paramter.Constants[8]["Value"] = tableSelect[0];
            }
          }
          return this.paramter;
        case "本表":
          if (type === "folder") {
            if (tableData.length > 0 && tableSelect.length === 0) {
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = tableData.map(v => v.Record_ID);
            } else if (tableData.length > 0 && tableSelect.length > 0) {
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = tableSelect.map(v => v.Record_ID);
            }
          } else if (type === "file") {
            if (childTableData.length > 0 && childTableSelect.length === 0) {
              numObj.juannei = childTableData.length;
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = childTableData.map(v => v.Record_ID);
            } else if (childTableData.length > 0 && childTableSelect.length > 0) {
              numObj.juannei = childTableSelect.length;
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = childTableSelect.map(v => v.Record_ID);
            }
          }
          return this.paramter;
        case "本表子级":
          if (type === "folder") {
            if (tableData.length > 0 && tableSelect.length === 0) {
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = tableData.map(v => v.Record_ID);
            } else if (tableData.length > 0 && tableSelect.length > 0) {
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = tableSelect.map(v => v.Record_ID);
            }
          } else if (type === "file") {
            if (childTableData.length > 0 && childTableSelect.length === 0) {
              numObj.juannei = childTableData.length;
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = childTableData.map(v => v.Record_ID);
            } else if (childTableData.length > 0 && childTableSelect.length > 0) {
              numObj.juannei = childTableSelect.length;
              await this.getConstants(numObj); // 添加常量
              this.paramter.List = childTableSelect.map(v => v.Record_ID);
            }
          }
          return this.paramter;
        case "分类目录":
          break;
        case "其他":
          break;
        default:
          break;
      }
    } catch (err) {
      console.log(err);
    }
  }
  /**
   * @名字: 获取常量字段和字段值
   * @详情:
   * @参数: self代表上文this,numObj:{juannei:'',fujian:''}
   * @返回值:
   */
  async getConstants(numObj: any) {
    const { visibleConfig, tableData } = this.dynamicStore;
    const { catalogList, treeNode } = this.catalogStore;
    const { fondsObj } = this.fondsStore;
    console.log("获取参数", tableData);
    const { FM_ID, Fonds_ID } = fondsObj;
    const { Catalog_ID } = treeNode;

    const item = catalogList.find(v => v.Catalog_ID === Catalog_ID);
    if (item) {
      // 分类名称
      this.paramter.Constants[4]["Value"] = item.Catalog_Name;
      // 分类号
      this.paramter.Constants[5]["Value"] = item.Catalog_Code;
    }
    // 存放FM_ID常量
    this.paramter.FM_ID = FM_ID;
    // 传输文件数量
    this.paramter.RowNumber = getSysConfig("RowNumber");
    // 获取归档方式
    // const item2 = await vueAjax.get("Sys_Base_FormModel/get", { searchList: [{ FM_ID: FM_ID }], flag: 2 });
    const item2 = await vueAxios.post(
      "Master_Base_FormModel/GetConditional",
      {
        order: "FM_Index asc",
        dtoList: [
          {
            fieldName: "FM_ID",
            fieldValue: FM_ID,
            conditionalType: 0
          }
        ]
      },
      { noLoading: true }
    );
    // 门类代号
    this.paramter.Constants[2]["Value"] = item2.List[0].FM_OEName;
    // 门类名称
    this.paramter.Constants[3]["Value"] = item2.List[0].FM_OCName;

    // 获取全宗数据
    // const item3 = await vueAjax.get("Sys_Base_Fonds/get", { searchList: [{ Fonds_ID: Fonds_ID }], flag: 2 });
    // const item3 = await vueAxios.get("Master_Base_Permission_Fonds/GetById", { objId: Fonds_ID }, { noLoading: true });
    // console.log(fondsObj);

    // 全宗号
    this.paramter.Constants[0]["Value"] = fondsObj.Fonds_ArchivalCode;
    // 全宗名
    this.paramter.Constants[1]["Value"] = fondsObj.Name;

    // 卷内数量
    this.paramter.Constants[6]["Value"] = numObj.juannei;
    // 附件数量
    this.paramter.Constants[7]["Value"] = numObj.fujian;
    // 父级数据
    this.paramter.Constants[8]["Value"] = tableData.length > 0 ? tableData[0] : "";
  }
}

export default ReportForm;
